﻿using Engine;
using Engine.Graphics;
using Game;
using GameEntitySystem;
using Neorxna;
using Neorxna.Common;
using System.Collections.Generic;
using TemplatesDatabase;

namespace NekoMeko.Components
{
	public class ComponentNekoHUD : Component, IUpdateable
	{
		public ComponentPlayer ComponentPlayer
		{
			get; set;
		}
		public ComponentNekoMekoModel ComponentNekoMekoModel
		{
			get; set;
		}
		public UpdateOrder UpdateOrder => UpdateOrder.FirstPersonModels;
		readonly List<Widget> m_widgets = [];
		NekoMekoPlayerModelWidget m_playerModel;
		bool m_initialized;

		public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
		{
			base.Load(valuesDictionary, idToEntityMap);
			ComponentPlayer = Entity.FindComponent<ComponentPlayer>();
			ComponentNekoMekoModel = Entity.FindComponent<ComponentNekoMekoModel>();
			m_initialized = false;
		}
		public override void OnEntityRemoved()
		{
			base.OnEntityRemoved();
			RemoveWidgets();
		}
		public void Initialize()
		{
			AddWidgets();
			m_initialized = true;
		}
		public void Update(float dt)
		{
			if (!m_initialized)
				Initialize();
			string dollMode = NeorxnaSettingOptions.Settings.GetValue("NekoMekoDollMode", "None");
			if (dollMode != "None")
			{
				if (!ComponentNekoMekoModel.TargetComponent.IsVisibleForCamera)
					ComponentNekoMekoModel.Animate();//当模型不可见时强制加载动画计算
				m_playerModel.BoneTransforms = ComponentNekoMekoModel.BoneTransforms;
				m_playerModel.BoneTransforms[ComponentNekoMekoModel.Model.RootBone.Name] = Matrix.Identity;
				m_playerModel.IsVisible = true;
				float width = 100f;
				float ratio = 1.8f;
				width *= NeorxnaSettingOptions.Settings.GetValue("NekoMekoDollScale", 1f);
				if (dollMode == "Bust")
				{//半身照
					m_playerModel.CameraShot = PlayerModelWidget.Shot.Bust;
					m_playerModel.Size = new Vector2(width);
					m_playerModel.ClampToBounds = true;
				}
				else
				{//全身照
					m_playerModel.CameraShot = PlayerModelWidget.Shot.Body;
					m_playerModel.Size = new Vector2(width, width * ratio);
					m_playerModel.ClampToBounds = false;
				}
				string align = NeorxnaSettingOptions.Settings.GetValue("NekoMekoDollAlign", "LeftTop");
				switch (align)
				{
					case "LeftTop":
					case "LeftBottom":
						m_playerModel.HorizontalAlignment = WidgetAlignment.Near; break;
					case "RightTop":
					case "RightBottom":
						m_playerModel.HorizontalAlignment = WidgetAlignment.Far; break;
				}
				switch (align)
				{
					case "LeftTop":
					case "RightTop":
						m_playerModel.VerticalAlignment = WidgetAlignment.Near; break;
					case "LeftBottom":
					case "RightBottom":
						m_playerModel.VerticalAlignment = WidgetAlignment.Far; break;
				}
				m_playerModel.Margin = new Vector2(NeorxnaSettingOptions.Settings.GetValue("NekoMekoDollMarginX", 72f),
					NeorxnaSettingOptions.Settings.GetValue("NekoMekoDollMarginY", 16f));
			}
			else
			{
				m_playerModel.BoneTransforms.Clear();
				m_playerModel.IsVisible = false;
			}
		}

		void AddWidgets()
		{
			m_playerModel = new NekoMekoPlayerModelWidget
			{
				PlayerClass = PlayerClass.Male,
				CharacterSkinTexture = ContentManager.Get<Texture2D>("Textures/Creatures/HumanMale1"),
				PlayerData = ComponentPlayer.PlayerData,
			};
			m_widgets.Add(m_playerModel);
			var parent = ComponentPlayer.GameWidget.Children.Find<CanvasWidget>("ControlsContainer");
			foreach (var child in m_widgets)
				parent.AddChildren(child);
		}
		void RemoveWidgets()
		{
			var parent = ComponentPlayer.GameWidget.Children.Find<CanvasWidget>("ControlsContainer");
			foreach (var child in m_widgets)
				parent.RemoveChildren(child);
		}

	}
}
